package ru.miit.elearning.vtutor.ftp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FTPClientDecorator {
//    private static long time = 0;
//    private static int counter = 0;

    public FTPClientDecorator() {
        ftp = new FTPClient();
        this.listMap = new HashMap<String, String[]>();
    }
    protected FTPClient ftp;
    protected Map<String, String[]> listMap;
    protected Map<String, FtpEntry> entryMap;

    public Map<String, FtpEntry> getEntryMap() {
        return entryMap;
    }

    public void setEntryMap(Map<String, FtpEntry> entryMap) {
        this.entryMap = entryMap;
    }

    public FTPFile[] listFiles() throws IOException {
                return ftp.listFiles();

//long s = System.currentTimeMillis();
//FTPFile[] files = ftp.listFiles();
//time += (System.currentTimeMillis() - s);
//counter++;
//return files;
    }

    public Map<String, String[]> getListMap() {
        return this.listMap;
    }

    public void clearListMap() {
        this.listMap.clear();
    }

    // temporary
    public int getReplyCode() {
        return ftp.getReplyCode();
    }
    //temporary
    public String[] list() throws IOException {
        return listNames();
    }

    public String[] listNames() throws IOException {
        return ftp.listNames();

//long s = System.currentTimeMillis();
//String[] names = ftp.listNames();
//time += (System.currentTimeMillis() - s);
//counter++;
//return names;
    }



    public String[] listPDS(String path) throws IOException {
        String[] list = null;

        String upperPath = path.toUpperCase();
        try {

            if (!listMap.containsKey(upperPath)) {
//long s1 = System.currentTimeMillis();
                list = ftp.listNames(upperPath + "(*)");
//time += (System.currentTimeMillis() - s1);
//counter++;
                if (list != null) {
                    for (int i = 0; i < list.length; i++) {
                        if (list[i] != null) {
                            list[i] = list[i].split("\\(")[1].split("\\)")[0];
                        }
                    }
                }
                listMap.put(upperPath, list);
            } else {
                list = listMap.get(upperPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public boolean retrieveFile(String file, OutputStream os) throws IOException {
        return ftp.retrieveFile(file, os);

//long s = System.currentTimeMillis();
//boolean retr = ftp.retrieveFile(file, os);
//time += (s - System.currentTimeMillis());
//counter++;
//return retr;
    }

    public boolean cd(String path) throws IOException {
        return ftp.changeWorkingDirectory(path);

//long s = System.currentTimeMillis();
//boolean cd = ftp.changeWorkingDirectory(path);
//time += (System.currentTimeMillis() - s);
//counter++;
//return cd;
    }

    public void connect(String ftpFullUrl) throws SocketException, IOException {
        String ftpHost = ftpFullUrl.split("@")[1];
        String ftpLogin = ftpFullUrl.split(":")[0];
        String ftpPassword = ftpFullUrl.split(":")[1].split("@")[0];

        ftp.connect(ftpHost);
        ftp.login(ftpLogin, ftpPassword);
    }

    public void close() throws IOException {
        if (ftp.isConnected()) {
//long s = System.currentTimeMillis();
            ftp.disconnect();
//time += (System.currentTimeMillis() - s);
//counter++;
//System.out.println(time);
//System.out.println(counter);
//time = 0;
//counter = 0;
        }
    }

    public boolean isConnected() {
        return ftp.isConnected();
    }

    public FTPClient getFTPClient() {
        return ftp;
    }



//    public String[] list() throws IOException {
//        String currentDir = ftp.printWorkingDirectory();
//        String[] list = null;
//        if (getListMap().containsKey(currentDir)) {
//            list = getListMap().get(currentDir);
//        } else {
//            list = ftp.listNames();
//            getListMap().put(currentDir, list);
//        }
//        return list;
//    }
//
//    public String[] list(String path) throws IOException {
//        String[] list = null;
//        if (getListMap().containsKey(path)) {
//            list = getListMap().get(path);
//            System.out.println(path+" taken from map");
//        } else {
////            String currecntDir = ftp.printWorkingDirectory();
//            ftp.changeWorkingDirectory(path);
//            list = ftp.listNames();
////            ftp.changeWorkingDirectory(currecntDir);
//            getListMap().put(path, list);
//            System.out.println(path+" put into map");
//        }
//        return list;
//    }
//    public int getReplyCode() {
//        return ftp.getReplyCode();
//    }
//
//    public String[] getReplies() {
//        return ftp.getReplyStrings();
//    }

}
